package com.smartfactory.sail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartfactory.sail.entity.Waste;
import com.smartfactory.sail.mapper.WasteMapper;
import com.smartfactory.sail.service.IWasteService;
import com.smartfactory.sail.utils.PageUtils;
import com.smartfactory.sail.vo.PageParam;
import com.smartfactory.sail.vo.waste.WasteList;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 废品库 服务实现类
 * </p>
 *
 * @author 王巨壁
 * @since 2019-08-27
 */
@Service
public class WasteServiceImpl extends ServiceImpl<WasteMapper, Waste> implements IWasteService {

    private WasteMapper wasteMapper;

    public WasteServiceImpl(WasteMapper wasteMapper) {
        this.wasteMapper = wasteMapper;
    }

    @Override
    public IPage<WasteList> listBypage(PageParam pageParam, Map<String, Object> queryParam) {
        Page<WasteList> page = new Page<>();
        pageParam.setSortField("status");
        PageUtils.createPage(page, pageParam);
        QueryWrapper<WasteList> query = new QueryWrapper<>();
        query.allEq(queryParam);
        return wasteMapper.listBypage(page,query);
    }

    @Override
    public int wastePreSum(long itemId, long orderId, long workId) {
        LambdaQueryWrapper<Waste> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Waste::getItemId, itemId);
        queryWrapper.eq(Waste::getOrderId, orderId);
        queryWrapper.le(Waste::getWorkId, workId);
        List<Waste> wastes = wasteMapper.selectList(queryWrapper);
        return wastes.stream().mapToInt(Waste::getSize).sum();
    }

    @Override
    public int wasteSize(long itemId, long orderId, long workId) {
        LambdaQueryWrapper<Waste> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Waste::getItemId, itemId);
        queryWrapper.eq(Waste::getOrderId, orderId);
        queryWrapper.eq(Waste::getWorkId, workId);
        List<Waste> wastes = wasteMapper.selectList(queryWrapper);
        return wastes.stream().mapToInt(Waste::getSize).sum();
    }
}
